#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <memory.h>

#define MAXN 100005
using namespace std;

int n, m, ans;
int w[MAXN], F[MAXN], vis[MAXN], out[MAXN];
vector<int> G[MAXN];

void TopSort() {
	memset(F, -0x3f, sizeof(F));


	queue<int> q;

    for (int i = 1; i <= n; ++i) {
        if (vis[i] == 0) {
            q.push(i);
            F[i] = w[i];
        }
    }

	while (!q.empty()) {
        int u = q.front();

		q.pop();
        for (unsigned i = 0; i < G[u].size(); ++i) {
            int v = G[u][i];
            F[v] = max(F[v], F[u] + w[v]);
            vis[v]--;
            if (vis[v] == 0) {
            	q.push(v);
			} 
        }
    }
}

int main() {
	while (cin >> n >> m) {
		for (int i = 1; i <= n; ++i) {
			cin >> w[i];
		}
		int a, b;
		for (int i = 1; i <= m; ++i) {
			scanf ("%d%d", &a, &b);
			G[a].push_back(b);
			++vis[b];
			++out[a];
		}
		TopSort();

		ans = 0;
		for (int i = 1; i <= n; ++i) {
			if (out[i] == 0) {
				ans = max(ans, F[i]);
			}
		}

		cout << ans <<endl;
		memset(vis, 0, sizeof(vis));
		memset(out, 0, sizeof(out));
		for (int i = 0; i <= n; ++i) {
			G[i].clear();
		}
	}
	return 0;
}
